<?php
	include '../init.php';
	
	$data = array(
		'name'=>addslashes($_POST['name']),
		'pid'=>addslashes($_POST['pid']),
		'keyword'=>addslashes($_POST['keyword']),
		'description'=>addslashes($_POST['description']),
		'add_time'=>time(),
		'status'=>(int)$_POST['status'],
		);
	
	if ($_POST['pid']==0) {
		$data['level']=1;
	}else{
		$pid=$_POST['pid'];
		$sql = "SELECT level FROM category WHERE id={$pid}";
		$r= $mysql -> getOne($sql);
		if ($r) {
			$data['level']=$r+1;
		}
	}
	//根据是否得到POST值，更新或者添加分类
	if (isset($_POST['id'])&&(int)$_POST['id']>0) {
		$id = (int)$_POST['id'];
		$res = $mysql -> update('category',$data,"id={$id}");
		if ($res>0) {
			echo json_encode(array('code'=>'000','msg'=>'保存成功'));
		}else{
			echo json_encode(array('code'=>'400','msg'=>'保存失败！'));
		}
	}else{
		$id = $mysql->addRow('category',$data);
		if ($id>0) {
			echo json_encode(array('code'=>'000','msg'=>'保存成功'));
		}else{
			echo json_encode(array('code'=>'400','msg'=>'保存失败！'));
		}
	
	}
	

	
?>